package cn.hinglo.common.ratelimit.config;

import cn.hinglo.common.ratelimit.annotation.EnableConcurrencyLimit;
import cn.hinglo.common.ratelimit.annotation.EnablePreIpLimit;
import cn.hinglo.common.ratelimit.annotation.EnableRequestLimit;
import cn.hinglo.common.ratelimit.annotation.EnableUserLimit;
import lombok.Data;

import java.io.Serializable;


/**
 * @author HingLo
 * @createTime: 2018/9/25 10:17
 * @description: 限流信息
 */
@Data
public class LimitationInfo implements Serializable {

    /**
     * 限流类型
     */
    private String type;

    /**
     * 请求通过次数
     */
    private int rate;

    /**
     * 制定时间
     */
    private int second;

    /**
     * 被限流后给用户的反馈操作 支持：json，render，text，redirect
     *
     * @return
     */
    private String renderType;

    /**
     * 被限流后给客户端的响应，响应的内容根据 action 的类型来渲染
     */
    private String renderContent;

    public LimitationInfo(EnableConcurrencyLimit limit) {
        this.type = LimitType.TYPE_CONCURRENCY;
        this.rate = limit.rate();
        this.second = limit.second();
        this.renderType = limit.renderType();
        this.renderContent = limit.renderContent();
    }

    public LimitationInfo(EnablePreIpLimit limit) {
        this.type = LimitType.TYPE_IP;
        this.rate = limit.rate();
        this.second = limit.second();
        this.renderType = limit.renderType();
        this.renderContent = limit.renderContent();
    }

    public LimitationInfo(EnableRequestLimit limit) {
        this.type = LimitType.TYPE_REQUEST;
        this.rate = limit.rate();
        this.second = limit.second();
        this.renderType = limit.renderType();
        this.renderContent = limit.renderContent();
    }

    public LimitationInfo(EnableUserLimit limit) {
        this.type = LimitType.TYPE_USER;
        this.rate = limit.rate();
        this.second = limit.second();
        this.renderType = limit.renderType();
        this.renderContent = limit.renderContent();
    }

}
